/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/

/**
 * @module jgrouse.widgets.WidgetProxy
 * @requires jgrouse.widgets.TemplateParser
 * @requires jgrouse.widgets.Container
 */
jgrouse.module({
	name : 'jgrouse.widgets.WidgetProxy',
	
	requires : ['jgrouse.widgets.TemplateParser', 'jgrouse.widgets.Container'],
	imports : ['jgrouse.dom.Element'],
	body : function(embed)
	{
		eval(embed);
	
		/**
		 * Container that acts as a wrapper/proxy around another widget
		 * @class jgrouse.widgets.WidgetProxy
		 * @extends jgrouse.widgets.Container
		 */	
		jgrouse.define('jgrouse.widgets.WidgetProxy', 'jgrouse.widgets.Container', function(embed)
		{
			eval(embed);
			
			return {
				/**
				 * @constructor initialize
				 * @inheritdesc
				 */
				initialize : function(options, styles)
				{
					_sup(this, arguments);
					this._content = null;
				},
				
				/**
				 * Get proxy's own template
				 * @function {String} getOwnTemplate
				 * @returns HTML template for the widget
				 */
				getOwnTemplate : function()
				{
					return null;	
				},
				
				/**
				 * Set node that would be used to hold widgets that are beiing wrapped
				 * @function setContent
				 * @param {DOMElement} content - DOM node containing items to be wrapped 
				 */
				setContent : function(content)
				{
					this._content = content;
				},
				
				/**
				 * @function setTemplate
				 * @inheritdesc
				 */
				setTemplate : function(template)
				{
					var ownTemplate = this.getOwnTemplate();
					if (!ownTemplate)
					{
						ownTemplate = '';
					}
					_super(this, 'setTemplate', ownTemplate);
					if (ownTemplate)
					{
						if (!this._content)
						{
							throw "Content node has not been defined";
						}
						if (this._content instanceof jgrouse.widgets.Widget)
						{
							throw "Content node cannot be a widget";
						}
						Element.forEach(this._content, function(node, index)
						{
							if (node.nodeType == Element.ELEMENT_NODE)
							{
								throw "Content node must be empty";
							}
						});
						this._content.innerHTML = '';	
					}
					
				},
				
				/**
				 * @function add
				 * @inheritdesc
				 */
				add : function(parentDom, refPoint)
				{
					var targetPoint = this._content? this._content : this.getDomElement();
					Element.moveChildrenToNewParent(targetPoint, refPoint);
					_sup(this, arguments);
				},
				
				/**
				 * @function remove
				 * @inheritdesc
				 */
				remove : function(doDispose)
				{
					var targetPoint = this._content? this._content : this.getDomElement();
					Element.moveChildrenToNewParent(this._refPoint, targetPoint);
				},
				
				getTargetNode : function()
				{
					return this._content? this._content : this.getDomElement();
				}
			
			}
		})
		
		
	}

});